var addTypeFlag=true;
var addFlag=true;
var dataDictionary = {
    // 编辑字典编码字段,标识是否改变了字典编码
    cacheGeneralCode : "",

    init : function() {
        //初始化按钮
        setMenuButtonOpermission();
        
        dataDictionary.initTypeCombobox();
        // 初始化事件绑定
        dataDictionary.evenBind();
        // 初始化数据加载
        dataDictionary.loadData();

    },
    evenBind : function() {
        $("#clear-btn").bind('click', function() {
            $("#generalName").textbox('clear');
            $("#generalCode").textbox('clear');
            $("#categoryName").textbox('clear')
        });
        $("#search-btn").bind('click', function() {
        	dataDictionary.loadData();
        });

        // 批量删除
        $("#deletes").bind('click', function() {
            var generalCodes = $('#listDataEx1').datagrid("getChecked");
            if (!generalCodes || generalCodes.length === 0) {
                $.messager.alert('提示', '请先选中记录!', 'warning');
                return;
            }
            var ids = generalCodes[0].generalCode;
            for (var i = 1; i < generalCodes.length; i++) {
                ids += "," + generalCodes[i].generalCode;
            }
            dataDictionary.deleteDictionary(ids);
        });

        // ----------------------------------------------------------------------->
        // 添加字典类型
        // ----------------------------------------------------------------------->
        // 取消
        $("#cancelCreateDataDictionaryCategory").bind('click', function() {
            $('#createDataDictionaryCategoryForm').form('clear');
            $('#createDataDictionaryCategoryDiv').dialog('close');
        });

        // 提交
        $('#submitCreateDataDictionaryCategory').bind('click', function() {
        	if(addTypeFlag){
        		addTypeFlag=false;
        		$('#createDataDictionaryCategoryForm').form('submit', {
                    url : getProjectUrl('/bfs/configuration/dictionarytype'),
                    onSubmit : function() {
                    	addTypeFlag=true;
                        var _flag = $(this).form('validate');
                        if (!_flag) {
                            return false;
                        }
                        var categoryClass = $('#addCategoryClass').val();
                        var _valid = dataDictionary.validCategoryClass(categoryClass);
                        if (!_valid) {
                        	$.messager.alert('提示', '字典类型编码已存在或为空!', 'warning');
                        	return false;
                        }
                        var categoryName = $('#addCategoryName').val();
                        var _valid = dataDictionary.validCategoryName(categoryName);
                        if (!_valid) {
                        	$.messager.alert('提示', '字典类型名称已存在或为空!', 'warning');
                        	return false;
                        }
                        addTypeFlag=false;
                        return true;
                    },
                    success : function(data) {
                        $('#createDataDictionaryCategoryForm').form('clear');
                        $('#createDataDictionaryCategoryDiv').dialog('close');
                        dataDictionary.initTypeCombobox();
                        addTypeFlag=true;
                    }
                });
        	}
        });

        // 添加类型按钮事件
        $('#addType').bind('click', function() {
            $('#createDataDictionaryCategoryDiv').window('open');
        });

        // ----------------------------------------------------------------------->
        // 添加字典
        // ----------------------------------------------------------------------->
        $('#add').bind('click', function() {
        	$('#createDataDictionaryDiv').window({
                 onOpen : function() {
                	 //dataDictionary.loadData();
                    $('#createDataDictionaryForm').form('load', {
                        generalCode : "",
                        generalName : "",
                        categoryClass : "",
                        cstatus : "1",
                        remark : ""
                    });
                }
            });
            $('#createDataDictionaryDiv').window('open');
        });

        // 取消
        $('#cancelCreateDataDictionary').bind('click', function() {
            $('#createDataDictionaryForm').form('clear');
            $('#createDataDictionaryDiv').dialog('close');
        });

        // 提交
        $('#submitCreateDataDictionary').bind('click', function() {
        	if(addFlag){
        		addFlag=false;
        		$("#categoryClass").val($('#selcategoryName').combobox("getValue"));
                $('#createDataDictionaryForm').form('submit', {
                    url : getProjectUrl('/bfs/configuration/dictionaryadd'),
                    onSubmit : function() {
                    	addFlag=true;
                        var _flag = $(this).form('validate');
                        if (!_flag) {
                            return false;
                        }
                        var generalCode = $('#addGeneralCode').val();
                        var _valid = dataDictionary.validGeneralCode(generalCode);
                        if (!_valid) {
                            $.messager.alert('提示', '字典编码已存在或为空!', 'warning');
                            return false;
                        }
                        addFlag=false;
                        return true;
                    },
                    success : function(data) {
                        $('#createDataDictionaryForm').form('clear');
                        $('#createDataDictionaryDiv').dialog('close');
                        dataDictionary.loadData();
                        addFlag=true;
                    }
                });
        	}
        });
        // ----------------------------------------------------------------------->
        // 修改字典
        // ----------------------------------------------------------------------->
        // 取消
        $('#cancelEditDataDictionary').bind('click', function() {
            $('#editDataDictionaryForm').form('clear');
            $('#editDataDictionaryDiv').dialog('close');
        });

        // 提交
        $('#submitEditDataDictionary').bind('click', function() {
            var newGeneralCode = $("#editGeneralCode").textbox('getValue');
            if (newGeneralCode !== dataDictionary.cacheGeneralCode) {
                var generalCode = $('#editGeneralCode').val();
                var _valid = dataDictionary.validGeneralCode(generalCode);
                if (!_valid) {
                    $.messager.alert('提示', '字典编码已存在或为空!', 'warning');
                    return;
                }
                var msg = '您确定要修改字典编码吗?';
                $.messager.confirm('提示', msg, function(r) {
                    if (r) {
                        $('#editDataDictionaryForm').form('submit', {
                            url : getProjectUrl('/bfs/configuration/dictionaryedit'),
                            onSubmit : function(param) {
                                param.oldGeneralCode = dataDictionary.cacheGeneralCode;
                                return $(this).form('validate');
                            },
                            success : function(data) {
                                $('#editDataDictionaryForm').form('clear');
                                $('#editDataDictionaryDiv').dialog('close');
                                $('#listDataEx1').datagrid('reload');
                            }
                        });
                    }
                });

            } else {
                $('#editDataDictionaryForm').form('submit', {
                    url : getProjectUrl('/bfs/configuration/dictionaryedit'),
                    onSubmit : function() {
                        return $(this).form('validate');
                    },
                    success : function(data) {
                        $('#editDataDictionaryForm').form('clear');
                        $('#editDataDictionaryDiv').dialog('close');
                        $('#listDataEx1').datagrid('reload');
                    }
                });
            }
        });

    },
    // 修改
    editDataDictionary : function(generalCode, generalName, categoryClass, cstatus,remark,orderNum) {

        dataDictionary.cacheGeneralCode = generalCode;
        $('#editDataDictionaryDiv').window({
            onOpen : function() {

                $('#editDataDictionaryForm').form('load', {
                    generalCode : generalCode,
                    generalName : generalName,
                    categoryClass : categoryClass,
                    cstatus : cstatus,
                    remark : remark,
                    orderNum:orderNum
                });
            }
        });
        $('#editDataDictionaryDiv').window('open');
    },
    // 删除
    deleteDictionary : function(generalCode) {
        var _msg = '您确定要删除选中的记录吗?';
        $.messager.confirm('提示', _msg, function(r) {
            if (r) {
                ajaxProcess({
                    url : getProjectUrl('/bfs/configuration/dictionarydrop'),
                    data :  {
                        generalCodes : generalCode
                    },
                    requestType : 'POST',
                    traditional : true,
                    successHandle : function(data) {
                        $.resultHandler(data, function() {
                            $('#listDataEx1').datagrid('reload');
                        });
                    }
                });
            }
        });
    },
    // 验证字典的名称
    validGeneralCode : function(_generalCode) {
        var _flag;
        ajaxProcess({
            url : getProjectUrl("/bfs/configuration/validgeneral"),
            data :  {
                generalCode : _generalCode
            },
            requestType : 'POST',
            async : false,
            successHandle : function(data) {
                _flag = data.result;
            }
        });
        return _flag;
    },
    // 验证字典类型的编码
    validCategoryClass : function(_categoryClass) {
        var _flag;

        ajaxProcess({
            url : getProjectUrl("/bfs/configuration/validcategory"),
            requestType : 'POST',
            data : {
                categoryClass : _categoryClass
            },
            async : false,
            successHandle : function(data) {
                _flag = data.result;
            }
        });

        return _flag;
    },
    // 验证字典类型的名称
    validCategoryName : function(_categoryName) {
    	var _flag;
    	
    	ajaxProcess({
    		url : getProjectUrl("/bfs/configuration/validcategoryname"),
    		requestType : 'POST',
    		data : {
    			categoryName : _categoryName
    		},
    		async : false,
    		successHandle : function(data) {
    			_flag = data.result;
    		}
    	});
    	
    	return _flag;
    },
    // 初始化类型的下拉框
    initTypeCombobox : function() {
        $('#searchcategoryName').combobox({
            url : getProjectUrl('/bfs/configuration/categoryclass?type=0'),
            valueField : 'categoryClass',
            textField : 'categoryName',
            editable:false,
            onLoadSuccess : function() { // 加载完成后,设置选中第一项
                $(this).combobox('select', "");
            }
        });

        $('#selcategoryName').combobox({
            url : getProjectUrl('/bfs/configuration/categoryclass?type=1'),
            valueField : 'categoryClass',
            textField : 'categoryName',
            editable:false,
            onLoadSuccess : function() { // 加载完成后,设置选中第一项
                var val = $(this).combobox('getData');
                for ( var item in val[0]) {
                    if (item === 'categoryClass') {
                        $(this).combobox('select', val[0][item]);
                    }
                }
            }
        });

        $('#editCategoryName').combobox({
            url : getProjectUrl('/bfs/configuration/categoryclass?type=1'),
            valueField : 'categoryClass',
            textField : 'categoryName',
            editable:false
        });

    },
    loadData : function() {
    	$(".list").show();
		$(".nolist").hide();
		
        // ----------------------------------------------------------------------->
        // 加载全部数据字典
        // ----------------------------------------------------------------------->
        $('#listDataEx1').datagrid({
            url : getProjectUrl('/bfs/configuration/dictionary'),
            queryParams : {
            	generalName : $.trim($("#searchGeneralName").textbox('getValue')),
                categoryClass : $('#searchcategoryName').combobox("getValue"),
                generalCode : $("#searchGeneralCode").textbox('getValue')
            },
            columns : [ [ {
                field : 'id',
                checkbox : true
            }, {
                field : 'categoryName',
                title : '字典类型',
                width : 100
            }, {
                field : 'generalCode',
                title : '字典编码',
                width : 100
            }, {
                field : 'generalName',
                title : '字典名称',
                width : 200
            }, {
                field : 'remark',
                title : '备注',
                width : 200
            }, {
                field : 'cstatus',
                title : '状态',
                width : 100,
                formatter : function(value) {
                    if (value !== 1) {
                        return '<font color="red">停用</font>';
                    } else {
                        return '启用';
                    }
                }
            }, {
                field : 'operate',
                title : '操作',
                width : 100,
                formatter : function(value, rows) {
                    var editParam = '\'' + rows.generalCode + '\',\'' + rows.generalName + '\',\'' + rows.categoryClass + '\',\'' + rows.cstatus + '\',\'' + rows.remark + '\',\''+rows.orderNum+'\'';
                    var operate = '<a class="btn-edit" title="编辑" name="edit" onclick="dataDictionary.editDataDictionary(' + editParam + ');"></a>';
                    operate += '<a class="btn-delete" title="删除" name="delete" onclick="dataDictionary.deleteDictionary(\'' + rows.generalCode + '\');"></a>';
                    return operate;
                }
            } ] ],
            striped : true,
            rownumbers : true,
            method : 'POST',
            pagination : true,
            pageSize : 20,
            pageList : [ 20, 50, 100, 200 ],
            fitColumns : true,
            autoRowHeight : true,
            onLoadSuccess : function(data) {
                setMenuButtonOpermission();
                $('.menucls').linkbutton();
                if(data.rows === null || data.rows.length === 0){
					   $(".list").hide();
					   $(".nolist").css("height", $(".list").outerHeight()).show();	 
				}
            }
        });
    }
};

$(document).ready(function() {
    resetSize();
    dataDictionary.init();
});


